package com.hotel.controller;

import com.hotel.pojo.Role;
import com.hotel.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
@RequestMapping("/role")
public class RoleController {
    @Autowired
    private RoleService roleService;

    /**
     * 查询全部，返回到用户展示页面
     * @return
     */
    @RequestMapping("/all")
    public String getAll(Model model){
        List<Role> roleList = roleService.getAll();
        model.addAttribute("allRole",roleList);
        System.out.println(roleList);
        return "allRole";
    }

    /**
     * 跳转到增加用户页面
     */
    @RequestMapping("/toAddRole")
    public String toAddRole(){
        return "addRole";
    }

    //添加用户的请求
    @RequestMapping("/addRole")
    public String addRole(Role role){
        roleService.addRole(role);
        return "redirect:/role/all";//重定向
    }

    //跳转到修改页面
    @RequestMapping("/toUpdateRole")
    public String toUpdateRole(int id,Model model){
        Role role = roleService.selectRoleById(id);
        model.addAttribute("role",role);
        return "updateRole";
    }

    //修改用户的请求
    @RequestMapping("/updateRole")
    public String updateRole(Role role){
        roleService.updateRole(role);
        return "redirect:/role/all";//重定向
    }

    //删除用户的请求
    @RequestMapping("/deleteRole/{id}")
    public String deleteRole(@PathVariable("id") int id){
        roleService.deleteRoleById(id);
        return "redirect:/role/all";
    }

}
